import pytest
import requests

from api.recharge_api import RechargeApi
from api.reg_login_api import RegLoginApi
from common.util import build_account, html_parser
from config import path_cfg


class TestRecharge(object):

    def setup(self):
        self.session = requests.Session()
        self.reg_login_api = RegLoginApi(self.session)
        self.recharge_api = RechargeApi(self.session)

        phone = path_cfg.REG_LOGIN_PHONE
        build_account('recharge', self.session, phone)
        self.reg_login_api.user_login({"keywords": phone, "password": "a123456"})

    def teardown(self):
        self.session.close()

    # 获取充值验证码
    @pytest.mark.run(order=1)
    def test_get_recharge_verify_code(self):
        resp = self.recharge_api.get_img_verify_code(1)
        assert 200 == resp.status_code

    # 充值
    @pytest.mark.run(order=2)
    def test_recharge(self):
        self.recharge_api.get_img_verify_code(1)

        form_data = {
            "paymentType": "chinapnrTrust",
            "amount": "10000",
            "formStr": "reForm",
            "valicode": "8888"
        }
        resp = self.recharge_api.recharge(form_data)
        assert 200 == resp.status_code
        assert 200 == resp.json().get('status')

    # 第三方充值
    @pytest.mark.run(order=3)
    def test_third_recharge(self):
        self.recharge_api.get_img_verify_code(1)

        form_data = {
            "paymentType": "chinapnrTrust",
            "amount": "10000",
            "formStr": "reForm",
            "valicode": "8888"
        }
        resp = self.recharge_api.recharge(form_data)
        form_data = resp.json().get('description').get('form')
        parser_data = html_parser(form_data)
        third_recharge_resp = self.recharge_api.third_recharge(parser_data.get('url'), parser_data.get('req_body'))
        assert 'OK' in third_recharge_resp.text
